﻿using QuanLyBanHang.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace QuanLyBanHang.DAL
{
   public class ChiTietHoaDonDAL
    {
        private static KetNoiSQL kn = new KetNoiSQL();
        private static SqlCommand cmd = new SqlCommand();
        public static int ThemChiTietHoaDon(ChiTietHoaDonDTO cthd)
        {
            cmd.CommandText = "insert into ChiTietHD values (@MaHD,@MaSP,@SoLuong,@DonGia,@ThanhTien)";
            cmd.Parameters.Add("@MaHD", SqlDbType.NVarChar, 50).Value = ChiTietHoaDonDTO.MaHD;
            cmd.Parameters.Add("@MaSP", SqlDbType.NVarChar, 50).Value = ChiTietHoaDonDTO.MaSP;
            cmd.Parameters.Add("@SoLuong", SqlDbType.Int).Value = ChiTietHoaDonDTO.SoLuong;
            cmd.Parameters.Add("@ThanhTien", SqlDbType.Int).Value = ChiTietHoaDonDTO.ThanhTien;
            return kn.DongThucThi(cmd);
        }
        public static int XoaChiTietHD(ChiTietHoaDonDTO cthd)
        {
            
            cmd.CommandText = "delete ChiTietHD where MaHD=@MaHD and MaSP = @MaSP";
            cmd.Parameters.Add("@MaHD", SqlDbType.NVarChar, 50).Value = ChiTietHoaDonDTO.MaHD;
            cmd.Parameters.Add("@MaSP", SqlDbType.NVarChar, 50).Value = ChiTietHoaDonDTO.MaSP;

            return kn.DongThucThi(cmd);
        }
        public static int SuaChiTietHD(ChiTietHoaDonDTO cthd)
        {
           
            cmd.CommandText = "update ChiTietHD set SoLuong=@SoLuong, DonGia =@DonGia ,ThanhTien=@Thanhtien where  MaHD=@Mahd and MaSP = @MaSP ";
         
            cmd.Parameters.Add("@SoLuong", SqlDbType.Int).Value = ChiTietHoaDonDTO.SoLuong;
            cmd.Parameters.Add("@DonGia", SqlDbType.Int).Value = ChiTietHoaDonDTO.DonGia;
            cmd.Parameters.Add("@ThanhTien", SqlDbType.Int).Value = ChiTietHoaDonDTO.ThanhTien;
            return kn.DongThucThi(cmd);
        }
        public static DataTable LayTatCaThongTinChiTietHoaDon(ChiTietHoaDonDTO cthd)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from ChiTietHD";
            return kn.LayDuLieu(cmd);

        }
        //load du lieu len dataGRIDVIEW input la dieu kien
        public static DataTable LayTTChiTietHoaDonTheoDK(string dieukien)
        {
            return KetNoiSQL.LayDuLieuSelect("select * from ChiTietHoaDon " + dieukien);
        }
       //lay ma san phan 
        public static DataTable LayThongTinSanPham(string dieukien)
        {
            return KetNoiSQL.LayDuLieuSelect("select MaSP,TenSP,DonGia from SanPham " + dieukien);
        }
      
     
    }
}
